package com.ssi.mrms.action.room;

import static com.opensymphony.xwork2.Action.SUCCESS;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.ssi.mrms.action.BaseAction;
import com.ssi.mrms.model.Msg;
import com.ssi.mrms.model.RMactwModel;
import com.ssi.mrms.model.TowerModel;
import com.ssi.mrms.model.Msg.MsgType;
import com.ssi.mrms.service.ComboboxService;
import com.ssi.mrms.service.KeyCardServiec;
import com.ssi.mrms.utils.DateUtils;
import com.ssi.mrms.view.SelectBoxView;

@Namespaces(value = { @Namespace("/room") })
public class RM006AAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(RM006AAction.class);

    private ComboboxService comboboxService  = lookupBean(ComboboxService.class, "ComboboxService");

    
	private String tower;
	private String floor;
	private String unit;
	private String addr;

	private Integer rlrmidKey;

	private String cardNo;
	private String status;
	private String userNo;
	private String createDate;
	private String lastDate;
	private Integer actwid;

	private RMactwModel rMactwModel;
	
	private String mode ;
	
	// combobox
    private List<SelectBoxView> comboTower = null;
    private List<SelectBoxView> comboFloor = null;
    private List<SelectBoxView> comboUnit = null;
    private List<SelectBoxView> comboAddress = null;
    private TowerModel towerModel;
    
	
	private KeyCardServiec keyCardServiec = lookupBean(KeyCardServiec.class, "keyCardServiec");

	@Action(value = "/RM006A", results = { @Result(name = "success", location = "RM006A.tiles", type = "tiles") })
	public String execute() throws Exception {
		logger.info("## " + this.getClass().getSimpleName());
		return SUCCESS;
	}

	public RM006AAction() {
		logger.info("## ");
		comboTower = comboboxService.getTower();
		comboFloor = comboboxService.getFloor(null);
		comboUnit = comboboxService.getUnit(null);
		comboAddress = comboboxService.getAddress(null);
	}
	@Action(value = "/RM006Aadd", results = { @Result(name = "success", location = "RM006A.tiles", type = "tiles") })
	public String add() throws Exception {
		logger.info("## ");

		rMactwModel = new RMactwModel();
		rMactwModel.setActwcrdno(cardNo);
		rMactwModel.setActwstat(status);
		rMactwModel.setActwusrno(userNo);

		// room Id
		 rMactwModel.setActwid( actwid );
		 rMactwModel.setRlrmidKey( rlrmidKey );
		 rMactwModel.setActwid(actwid);

		// create date
		 rMactwModel.setActwcredat( DateUtils.getDateFormate(createDate) );
		// last date
		 rMactwModel.setActwupddat( DateUtils.getDateFormate( lastDate )  );
		 
		 logger.info("# " + tower);
		 logger.info("# " + floor);
		 logger.info("# " + unit);
		 logger.info("addr# " + addr);
		 towerModel = new TowerModel();
		 towerModel.setTower(tower);
		 towerModel.setFloor(floor);
		 towerModel.setUnitNo(unit);
		 towerModel.setAddress(addr);

		 Msg msg = new Msg();
		 try{
			 logger.info("########### mode : " + mode);
			 if( mode != null && mode.equalsIgnoreCase("edit")){
				 logger.info("actwid : " + actwid);
				 keyCardServiec.update(rMactwModel);
				 msg.setType(MsgType.SUCCESS);
				 msg.setMsg("SUCCESS");
				 
			 } else{
				 TowerModel r = keyCardServiec.findRoomId(towerModel);
				 if(r != null){
					 rMactwModel.setRlrmidKey(r.getRoomId());
					 keyCardServiec.insert(rMactwModel);
					 msg.setType(MsgType.SUCCESS);
					 msg.setMsg("SUCCESS");
				 }else{
					 logger.info("RoomID not found");
					 msg.setType(MsgType.ERROR);
					 msg.setMsg("Room not found");
				 }
			 }
			 
		 }catch(Exception e ){
			 e.printStackTrace();
			 msg.setType(MsgType.ERROR);
			 msg.setMsg(e.getMessage());
		 }
		 
		 setMsg(msg);
		 
		return SUCCESS;
	}
	@Action(value = "/RM006Aedit", results = { @Result(name = "success", location = "RM006A.tiles", type = "tiles") })
	public String edit() throws Exception {
		logger.info("##  edit");
		
		mode = "edit";
		if( actwid != null){
			RMactwModel re = keyCardServiec.find(actwid.toString());
			logger.info("## RMactwModel " + re);
			if(re != null){
				
			    rlrmidKey = re.getRlrmidKey();
				cardNo = re.getActwcrdno();
				status = re.getActwstat();
				userNo = re.getActwusrno();
				createDate = DateUtils.getDateStringFormate( re.getActwcredat());
				lastDate   = DateUtils.getDateStringFormate( re.getActwupddat());
				actwid = re.getActwid();
				
				rMactwModel = re;
				
			}
			
		}else{
			logger.info("## RMactwModel not found ");
		}
		
		return SUCCESS;
	}
	@Action(value = "/RM006Adelete", results = { @Result(name = "success", location = "/room/RM006", type = "redirect") })
	public String delelte() throws Exception {
		logger.info("### delelte");
			RMactwModel re = keyCardServiec.find(actwid.toString());
			if(re != null){
				keyCardServiec.delete(re);
			}else{
				logger.info("## RMactwModel not found ");
			}
		
		return SUCCESS;
	}

	public void setTower(String tower) {
		this.tower = tower;
	}

	public void setFloor(String floor) {
		this.floor = floor;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public void setLastDate(String lastDate) {
		this.lastDate = lastDate;
	}


	public Integer getRlrmidKey() {
		return rlrmidKey;
	}

	public void setRlrmidKey(Integer rlrmidKey) {
		this.rlrmidKey = rlrmidKey;
	}

	public RMactwModel getrMactwModel() {
		return rMactwModel;
	}

	public void setrMactwModel(RMactwModel rMactwModel) {
		this.rMactwModel = rMactwModel;
	}

	public String getTower() {
		return tower;
	}

	public String getFloor() {
		return floor;
	}

	public String getUnit() {
		return unit;
	}

	public String getAddr() {
		return addr;
	}

	public String getCardNo() {
		return cardNo;
	}

	public String getStatus() {
		return status;
	}

	public String getUserNo() {
		return userNo;
	}

	public String getCreateDate() {
		return createDate;
	}

	public String getLastDate() {
		return lastDate;
	}

	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public Integer getActwid() {
		return actwid;
	}

	public void setActwid(Integer actwid) {
		this.actwid = actwid;
	}

	public List<SelectBoxView> getComboTower() {
		return comboTower;
	}

	public void setComboTower(List<SelectBoxView> comboTower) {
		this.comboTower = comboTower;
	}

	public List<SelectBoxView> getComboFloor() {
		return comboFloor;
	}

	public void setComboFloor(List<SelectBoxView> comboFloor) {
		this.comboFloor = comboFloor;
	}

	public List<SelectBoxView> getComboUnit() {
		return comboUnit;
	}

	public void setComboUnit(List<SelectBoxView> comboUnit) {
		this.comboUnit = comboUnit;
	}

	public List<SelectBoxView> getComboAddress() {
		return comboAddress;
	}

	public void setComboAddress(List<SelectBoxView> comboAddress) {
		this.comboAddress = comboAddress;
	}

	
}
